package com.toocms.jinjuhang.ui.index

import android.graphics.drawable.Drawable
import androidx.annotation.ColorInt
import androidx.core.graphics.drawable.DrawableCompat
import androidx.databinding.ObservableField
import androidx.databinding.ObservableFloat
import com.blankj.utilcode.util.ConvertUtils
import com.blankj.utilcode.util.ResourceUtils
import com.toocms.jinjuhang.R
import com.toocms.jinjuhang.config.Constants
import com.toocms.jinjuhang.ui.index.article_details.ArticleDetailsFgt
import com.toocms.tab.base.BaseViewModel
import com.toocms.tab.base.MultiItemViewModel
import com.toocms.tab.binding.command.BindingAction
import com.toocms.tab.binding.command.BindingCommand

class IndexCommunityItemModel(viewModel: BaseViewModel<*>, communityItemPosition: Int) :
    MultiItemViewModel<BaseViewModel<*>>(viewModel) {

    val rawBackground by lazy {
        ResourceUtils.getDrawable(R.drawable.shape_sol_ffffff_cor_10dp).mutate()
    }
    val backgroundObservableField = ObservableField<Drawable>()
    val paddingStartObservableFloat = ObservableFloat(0f)
    val paddingTopObservableFloat = ObservableFloat(0f)
    val paddingEndObservableFloat = ObservableFloat(0f)
    val paddingBottomObservableFloat = ObservableFloat(0f)

    val itemClickBindingCommand = BindingCommand<BindingAction>(BindingAction {
        viewModel.startFragment(ArticleDetailsFgt::class.java)
    })

    init {
        itemType = Constants.RecyclerViewItemType.ITEM_TYPE_EIGHT
        when (communityItemPosition % 3) {
            0 -> {
                backgroundObservableField.set(
                    changeDrawableColor(
                        rawBackground,
                        0xFFFDA04B.toInt()
                    )
                )
                paddingStartObservableFloat.set(ConvertUtils.dp2px(15f).toFloat())
                paddingEndObservableFloat.set(ConvertUtils.dp2px(5f).toFloat())
            }
            1 -> {
                backgroundObservableField.set(
                    changeDrawableColor(
                        rawBackground,
                        0xFF55BED9.toInt()
                    )
                )
                paddingStartObservableFloat.set(ConvertUtils.dp2px(10f).toFloat())
                paddingEndObservableFloat.set(ConvertUtils.dp2px(10f).toFloat())
            }
            2 -> {
                backgroundObservableField.set(
                    changeDrawableColor(
                        rawBackground,
                        0xFFFD552C.toInt()
                    )
                )
                paddingStartObservableFloat.set(ConvertUtils.dp2px(5f).toFloat())
                paddingEndObservableFloat.set(ConvertUtils.dp2px(15f).toFloat())
            }
        }
        if (2 < communityItemPosition) {
            paddingTopObservableFloat.set(ConvertUtils.dp2px(15f).toFloat())
        }
    }

    private fun changeDrawableColor(drawable: Drawable, @ColorInt color: Int) = drawable.apply {
        DrawableCompat.setTint(this, color)
    }
}